﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiangYeeProj
{
    public partial class FormSetting : Form
    {
        string dataFilePath;
        string filePath1;
        string filePath2;
        public FormSetting()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = Application.StartupPath;//@"c:\";   //@是取消转义字符的意思  
            fdlg.Filter = "EXE files(*.txt)|*.txt|All files(*.*)|*.*";
            /* 
             * FilterIndex 属性用于选择了何种文件类型,缺省设置为0,系统取Filter属性设置第一项 
             * ,相当于FilterIndex 属性设置为1.如果你编了3个文件类型，当FilterIndex ＝2时是指第2个. 
             */
            fdlg.FilterIndex = 1;
            /* 
             *如果值为false，那么下一次选择文件的初始目录是上一次你选择的那个目录， 
             *不固定；如果值为true，每次打开这个对话框初始目录不随你的选择而改变，是固定的   
             */
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fdlg.FileName;
                dataFilePath = fdlg.FileName;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(Repair);
            t.Start();
        }

        private void Repair()
        {
            DataManager.RepairLiangYeeDateTime(textBox1.Text, textBox2.Text);
        }

        private void CombineFile()
        {
            //file1
            FileStream rFS1 = new FileStream(filePath1, FileMode.Open);
            StreamReader sr1 = new StreamReader(rFS1);

            //newFile
            string newFilePath = Path.Combine(txtTraget.Text, Path.GetFileNameWithoutExtension(filePath1) + "Combin(Azure2011.1_2013.5)(Lingyee2013.5-2017.5).txt");//filePath1.Replace(".txt", "") + "Combin(Azure2011.1_2013.5)(Lingyee2013.5-2017.5).txt";
            FileStream wFS;
            wFS = new FileStream(newFilePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(wFS);


            Console.WriteLine("file1 start:");
            string line = string.Empty;
            long n = 0;
            while (sr1.Peek() > -1)
            {
                n++;
                line = sr1.ReadLine().Replace("\r", string.Empty).Replace("\n", string.Empty);
                int index = line.IndexOf('.');
                string date = line.Substring(0, index);
                string data = line.Substring(index + 1);

                DateTime lineDate = Convert.ToDateTime(date);
                sw.WriteLine(lineDate.ToString(txtTimeFormat.Text) + "." + data);
                if(n%300000==0)
                {
                    Console.WriteLine(line);
                    Console.WriteLine(lineDate.ToString(txtTimeFormat.Text) + "." + data);
                }
            }
            Console.WriteLine("total lines :{0};   and last line is {1}", n, line);
            sr1.Close();
            rFS1.Close();

            Console.WriteLine("file2 start:");
            FileStream rFS2 = new FileStream(filePath2, FileMode.Open);
            StreamReader sr2 = new StreamReader(rFS2);
            while (sr2.Peek() > -1)
            {
                n++;
                line = sr2.ReadLine().Replace("\r", string.Empty).Replace("\n", string.Empty);
                int index = line.IndexOf('.');
                string date = line.Substring(0, index);
                string data = line.Substring(index + 1);

                DateTime lineDate = Convert.ToDateTime(date);
                sw.WriteLine(lineDate.ToString(txtTimeFormat.Text) + "." + data);

                if (n % 300000 == 0)
                {
                    Console.WriteLine(line);
                    Console.WriteLine(lineDate.ToString(txtTimeFormat.Text) + "." + data);
                }
            }
            sr2.Close();
            rFS2.Close();

            sw.Flush();
            //关闭流
            sw.Close();
            wFS.Close();
            Console.WriteLine("Done!");
            //File.Move(newFilePath, newFilePath.Replace(".txt", "") + "_" + i + "lines.txt");
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            Thread t = new Thread(CombineFile);
            t.Start();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = Application.StartupPath;//@"c:\";   //@是取消转义字符的意思  
            fdlg.Filter = "EXE files(*.txt)|*.txt|All files(*.*)|*.*";
            /* 
             * FilterIndex 属性用于选择了何种文件类型,缺省设置为0,系统取Filter属性设置第一项 
             * ,相当于FilterIndex 属性设置为1.如果你编了3个文件类型，当FilterIndex ＝2时是指第2个. 
             */
            fdlg.FilterIndex = 1;
            /* 
             *如果值为false，那么下一次选择文件的初始目录是上一次你选择的那个目录， 
             *不固定；如果值为true，每次打开这个对话框初始目录不随你的选择而改变，是固定的   
             */
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = fdlg.FileName;
                filePath1 = fdlg.FileName;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = Application.StartupPath;//@"c:\";   //@是取消转义字符的意思  
            fdlg.Filter = "EXE files(*.txt)|*.txt|All files(*.*)|*.*";
            /* 
             * FilterIndex 属性用于选择了何种文件类型,缺省设置为0,系统取Filter属性设置第一项 
             * ,相当于FilterIndex 属性设置为1.如果你编了3个文件类型，当FilterIndex ＝2时是指第2个. 
             */
            fdlg.FilterIndex = 1;
            /* 
             *如果值为false，那么下一次选择文件的初始目录是上一次你选择的那个目录， 
             *不固定；如果值为true，每次打开这个对话框初始目录不随你的选择而改变，是固定的   
             */
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = fdlg.FileName;
                filePath2 = fdlg.FileName;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txtTraget.Text = path.SelectedPath;
        }
    }
}
